package com.aliwo.config;

import com.aliwo.constant.IllegalSqlUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.h2.Driver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import tech.powerjob.common.utils.CommonUtils;
import tech.powerjob.official.processors.impl.sql.DynamicDatasourceSqlProcessor;
import tech.powerjob.official.processors.impl.sql.SpringDatasourceSqlProcessor;
import tech.powerjob.official.processors.util.SecurityUtils;

import javax.sql.DataSource;

/**
 * @author:xuyayuan
 * @date:2021/7/26 15:44
 * @description:TODO
 */
@Configuration
public class SqlProcessorConfiguration {

    @Bean
    @DependsOn({"initPowerJob"})
    public DataSource sqlProcessorDataSource() {
        String path = System.getProperty("user.home") + "/test/h2/" + CommonUtils.genUUID() + "/";
        String jdbcUrl = String.format("jdbc:h2:file:%spowerjob_sql_processor_db;DB_CLOSE_DELAY=-1;" +
                "DATABASE_TO_UPPER=false", path);
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(Driver.class.getName());
        config.setJdbcUrl(jdbcUrl);
        config.setAutoCommit(true);
        // 池中最小空闲连接数量
        config.setMinimumIdle(1);
        // 池中最大连接数量
        config.setMaximumPoolSize(10);
        return new HikariDataSource(config);
    }

    @Bean
    public SpringDatasourceSqlProcessor simpleSpringSqlProcessor(@Qualifier("sqlProcessorDataSource") DataSource dataSource) {
        SpringDatasourceSqlProcessor springDatasourceSqlProcessor = new SpringDatasourceSqlProcessor(dataSource);
        // 排除掉包含 update 的 SQL
        springDatasourceSqlProcessor.registerSqlValidator("SpringDatasourceSqlProcessorInterceptIllegalSqlAllValidator",
                sql -> IllegalSqlUtil.isSqlValid(sql));

        // 排除掉包含 drop 的 SQL
        //springDatasourceSqlProcessor.registerSqlValidator("interceptDropValidator",
        //        sql -> sql.matches("^(?i)((?!drop).)*$"));

        // do nothing sql解析器
        springDatasourceSqlProcessor.setSqlParser((sql, taskContext) -> sql);
        return springDatasourceSqlProcessor;
    }

    @Bean
    public DynamicDatasourceSqlProcessor dySimpleSpringSqlProcessor(@Qualifier("sqlProcessorDataSource") DataSource dataSource) {
        // 开启 DynamicDatasourceSqlProcessor
        System.setProperty(SecurityUtils.ENABLE_DYNAMIC_SQL_PROCESSOR, Boolean.TRUE.toString());

        DynamicDatasourceSqlProcessor dynamicDatasourceSqlProcessor = new DynamicDatasourceSqlProcessor();
        // 排除掉包含 drop 的 SQL
        dynamicDatasourceSqlProcessor.registerSqlValidator("dyinterceptIllegalSqlAllValidator",
                sql -> IllegalSqlUtil.isSqlValid(sql));

        // sql解析器
        dynamicDatasourceSqlProcessor.setSqlParser((sql, taskContext) -> sql);

        return dynamicDatasourceSqlProcessor;
    }

}
